<template>
    <div class="shoppingcart">
        <van-nav-bar v-if="cartlist.list.length == 0" title="购物车" left-arrow @click-left="onClickLeft" @click-right="onClickRight" />
        <van-nav-bar v-else title="购物车" :right-text="isShow ? '完成' : '编辑'" left-arrow @click-left="onClickLeft" @click-right="onClickRight" />

        <div v-if="isLoaing" class="cart-box">
            <div class="loading">
                <van-loading type="spinner" />
            </div>
        </div>
        <div v-if="cartlist.list.length == 0 && !isLoaing" class="cart-box">
            <div class="cart-null">
                <i class="icon"></i>
                <p>您的购物车暂无商品</p>
                <van-button plain color="#373737" @click="goHome">随便逛逛</van-button>
            </div>
        </div>
        <div v-else class="cart-shopping">
            <div class="shopping-box" v-for="(item, index) in cartlist.list" :key="item.gid">
                <van-checkbox v-model="item.checked" @click="checked" checked-color="#353535"></van-checkbox>
                <div class="shopping">
                    <div class="shopping-img">
                        <!-- <img :src="`https://images.weserv.nl/?url=${item.goods_img}`" alt="" /> -->
                        <img :src="item.goods_img" alt="" />
                    </div>
                    <div v-show="isShow" class="shopping-detail">
                        <div class="title-box">
                            <div class="shopping-name">
                                <van-stepper v-model="item.goods_num" />
                                <div class="style-box" @click="showPopup(item)">
                                    <p>
                                        颜色:<span>{{ item.goods_color }}</span> 尺码:<span>{{ item.goods_size }}</span>
                                    </p>
                                </div>
                            </div>
                            <div class="shopping-num">
                                <p>×{{ item.goods_num }}</p>
                            </div>
                        </div>
                        <div class="price-box">
                            <p>￥{{ item.goods_price }}</p>
                        </div>
                    </div>
                    <div v-show="!isShow" class="shopping-detail">
                        <div class="title-box">
                            <div class="shopping-name">
                                <p class="name">{{ item.goods_name }}</p>
                                <div class="shopping-style">
                                    <p class="color">
                                        颜色:<span>{{ item.goods_color }}</span>
                                    </p>
                                    <p class="size">
                                        尺码:<span>{{ item.goods_size }}</span>
                                    </p>
                                </div>
                            </div>
                            <div class="shopping-num">
                                <p>×{{ item.goods_num }}</p>
                            </div>
                        </div>
                        <div class="price-box">
                            <p>￥{{ item.goods_price }}</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="price-all-box">
                <!-- <p>总计<span class="price-all">￥248.00</span>=商品金额<span class="price-all">￥248.00</span></p> -->
            </div>

            <div class="submit-box">
                <van-checkbox checked-color="#353535" @click="checkAll" v-model="checkedAll">全选</van-checkbox>
                <div v-show="isShow" class="right-box">
                    <div class="like">
                        <button>移入收藏夹</button>
                    </div>
                    <button class="submit" @click="removeCartShopping">删除</button>
                </div>
                <div v-show="!isShow" class="right-box">
                    <div class="how-much">
                        <p class="all-price">
                            总计:
                            <span class="price-all">￥{{ totalPrice }}</span>
                            (
                            <span class="num">{{ length }}</span>
                            件)
                        </p>
                        <p class="prompt">不含运费</p>
                    </div>

                    <button class="submit" @click="goOrder">结算</button>
                </div>
            </div>
        </div>
        <van-popup v-model:show="show" overlay closeable close-icon="close" position="bottom" :style="{ height: 'auto' }">
            <div class="pop-ups">
                <div class="infos">
                    <div class="basic-info">
                        <div class="thumb-img">
                            <!-- <img :src="`https://images.weserv.nl/?url=${goods.list.goods_img}`" alt="" /> -->
                            <img :src="goods.list.goods_img" alt="" />
                        </div>
                        <div class="text-info">
                            <p class="price">
                                <span class="sale-price">￥{{ goods.list.goods_price }}</span
                                ><span class="market-price">￥499</span>
                            </p>
                            <p class="choosed-info" v-if="color == '' && size == ''">请选择颜色尺码</p>
                            <p class="choosed-info" v-else>颜色:{{ color }} 尺码:{{ size }}</p>
                        </div>
                    </div>
                    <div class="chose-items">
                        <div class="block-list">
                            <p class="name">颜色</p>
                            <ul>
                                <li :class="color == item ? 'active' : ''" v-for="item in colorlist" @click="getColor(item)">
                                    <p>{{ item }}</p>
                                </li>
                                <!-- <li @click="getColor">
                                    <p>黑色</p>
                                </li>
                                <li @click="getColor">
                                    <p>灰色</p>
                                </li>
                                <li @click="getColor">
                                    <p>天蓝色</p>
                                </li> -->
                            </ul>
                        </div>
                        <div class="block-list">
                            <p class="name">尺码</p>
                            <ul>
                                <li :class="size == item ? 'active' : ''" v-for="item in sizelist" @click="getSize(item)">
                                    <p>{{ item }}</p>
                                </li>
                                <!-- <li @click="getSize">
                                    <p>L</p>
                                </li>
                                <li @click="getSize">
                                    <p>XL</p>
                                </li>
                                <li @click="getSize">
                                    <p>XXL</p>
                                </li>
                                <li @click="getSize">
                                    <p>XXXL</p>
                                </li> -->
                            </ul>
                        </div>
                        <div class="nums">
                            <span>数量</span>
                            <van-stepper v-model="goods.list.goods_num" input-width="40px" button-size="32px" />
                        </div>
                    </div>
                </div>
            </div>
            <button @click="save(goods.list.cart_id)">确定</button>
        </van-popup>
    </div>
</template>

<script lang="ts" setup>
import { getShoppingCart, addOrder, removeCart, updateShoppingCart } from "../../api/index";
import { ref, reactive, computed, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useUserInfo } from "../../store/user";
import { showToast } from "vant";

let router = useRouter();
let route = useRoute();
const store = useUserInfo();

let colorlist = ["白色", "黑色", "灰色", "天蓝色"];
let sizelist = ["M", "L", "XL", "XXL", "XXXL"];

let show = ref(false);
let isShow = ref(false);
let checkedAll = ref(false);

//购物车列表
let cartlist = reactive({
    list: [],
});

//记录点击的物品
let goods: any = reactive({
    list: {},
});
//记录数量
let nums = ref("1");
//记录颜色
let color = ref("");
//记录尺码
let size = ref("");

//返回上一级
const onClickLeft = () => history.back();
const onClickRight = () => {
    isShow.value = !isShow.value;
};
const showPopup = (item: any) => {
    goods.list = item;
    color.value = item.goods_color;
    size.value = item.goods_size;

    show.value = true;
};

const save = (cart_id) => {
    // nums.value = goods.list.goods_num;
    // color.value = goods.list.goods_color;
    // size.value = goods.list.goods_size;
    if (color.value == "") {
        showToast("请选择颜色");
        return;
    }
    if (size.value == "") {
        showToast("请选择尺码");
        return;
    }
    // goods.list.goods_num = nums.value;
    nums.value = goods.list.goods_num;
    goods.list.goods_color = color.value;
    goods.list.goods_size = size.value;
    // console.log(goods.list);
    let data = {
        cart_id,
        goods_color: color.value,
        goods_size: size.value,
        goods_num: nums.value,
    };
    updateShoppingCart(data).then((res: any) => {
        // console.log(data);
        console.log("1.0", res);
        if (res.code == 200) {
            showToast(res.msg);
        }
    });
};

const getColor = (c: string) => {
    // console.log(c);
    color.value = c;
};
const getSize = (s: string) => {
    // console.log(s);
    size.value = s;
};

//计算总价
const totalPrice = computed(() => {
    let amountAll = 0;
    cartlist.list.forEach((item: any) => {
        if (item.checked) {
            let amount = item.goods_price * item.goods_num;
            amountAll += amount;
        }
    });
    return amountAll;
});

//计算多少件
const length = computed(() => {
    let l = 0;
    cartlist.list.forEach((item: any) => {
        if (item.checked) {
            l += item.goods_num;
        }
    });
    return l;
});

//删除功能
const removeCartShopping = () => {
    let gidArr = [];
    cartlist.list.forEach((item: any) => {
        if (item.checked == true) {
            gidArr.push(item.gid);
        }
    });
    let gids = gidArr.toString();
    removeCart({ gids }).then((res: any) => {
        console.log(res);
        if (res.code == 200) {
            getShoppingCart().then((res: any) => {
                if (res.code == "200") {
                    let result = res.content;
                    result.forEach((item: any) => {
                        item.checked = false;
                    });
                    cartlist.list = result;
                }

                if (res.code == -1) {
                    showToast(res.msg);
                    localStorage.removeItem("auto_token");
                    localStorage.removeItem("user_info");
                    store.getInfo({});
                    router.push({
                        path: "/login",
                    });
                }
            });
        }
    });
};

// 去结算
const goOrder = () => {
    console.log(111);

    let isChecked = cartlist.list.every((item) => {
        return item.checked == false;
    });

    if (isChecked) {
        showToast("请选择商品");
        return;
    }

    let gidArr = [];
    cartlist.list.forEach((item: any) => {
        if (item.checked == true) {
            gidArr.push(item.gid);
            let data = {
                order_name: item.goods_name,
                order_price: item.goods_price,
                order_img: item.goods_img,
                order_color: item.goods_color,
                order_size: item.goods_size,
                order_num: item.goods_num,
                order_status: 1,
            };

            addOrder(data).then((res: any) => {
                console.log(res);

                if (res.code == 200) {
                    router.push("/order");
                }
            });
        }
    });
    let gids = gidArr.toString();
    removeCart({ gids });
};

let isLoaing = ref(true);
//获取购物车数据
getShoppingCart().then((res: any) => {
    if (res.code == "200") {
        let result = res.content;
        result.forEach((item: any) => {
            item.checked = false;
        });
        cartlist.list = result;
        isLoaing.value = false;
    }

    if (res.code == -1) {
        showToast(res.msg);
        localStorage.removeItem("auto_token");
        localStorage.removeItem("user_info");
        store.getInfo({});
        router.push({
            path: "/login",
        });
    }
});

//单选
const checked = () => {
    let result = cartlist.list;
    for (let i = 0; i < result.length; i++) {
        if (!result[i].checked) {
            checkedAll.value = false;
            return;
        }
        checkedAll.value = true;
    }
};
//全选
const checkAll = () => {
    if (checkedAll.value) {
        cartlist.list.forEach((item: any) => {
            item.checked = true;
        });
    }
    if (!checkedAll.value) {
        cartlist.list.forEach((item: any) => {
            item.checked = false;
        });
    }
};

//返回主页
const goHome = () => router.push({ path: "/" });

// const onSubmit = () => showToast("点击按钮");
</script>

<style lang="scss" scoped>
.shoppingcart {
    background: #f0f0f0;
    // height: 1000px;
}
.cart-box {
    height: 620px;
    padding-top: 30px;
    width: 50%;
    margin: 0 auto;
    text-align: center;

    .loading {
        text-align: center;
        margin-top: 100px;
    }
    .cart-null {
        i {
            margin: 0 auto;
            display: block;
            width: 100px;
            height: 100px;
            background-image: url(@/assets/cart.png);
            background-repeat: no-repeat;
            background-size: 100%;
        }
        p {
            padding: 15px 0;
            font-size: 14px;
            color: #999;
        }
    }
}
.cart-shopping {
    width: 100%;
    .shopping-box {
        width: 100%;
        margin-bottom: 10px;
        background: #fff;
        padding: 0 15px;
        display: flex;
    }
    .shopping {
        // flex-grow: 1;
        width: 95%;
        padding: 10px 0 10px 10px;
        display: flex;

        .shopping-img {
            width: 76px;
            // width: 35%;
            height: 94px;
            img {
                width: 100%;
                height: 100%;
            }
        }

        .shopping-detail {
            flex: 1;
            padding: 10px 0 0 10px;

            .title-box {
                display: flex;
                .shopping-name {
                    width: 90%;
                    .name {
                        width: 100%;
                        height: 38px;
                        font-size: 14px;
                    }

                    .shopping-style {
                        padding-top: 5px;
                        display: flex;
                        .color,
                        .size {
                            font-size: 12px;
                            color: #999;
                            padding-right: 10px;
                        }
                    }
                }
                .style-box {
                    position: relative;
                    margin-top: 5px;
                    width: 80%;
                    padding: 5px;
                    border: 1px solid #999;
                    p {
                        font-size: 12px;
                        color: #353535;

                        span {
                            padding-right: 10px;
                        }
                    }

                    &:after {
                        position: absolute;
                        top: 8px;
                        right: 6px;
                        content: "";
                        display: block;
                        width: 6px;
                        height: 6px;
                        transform: rotate(45deg);
                        border: 1px solid #353535;
                        border-left-color: transparent;
                        border-top-color: transparent;
                    }
                }
                .shopping-num {
                    padding: 5px 0 0 10px;
                    font-size: 14px;
                    color: #999;
                }
            }

            .price-box {
                p {
                    font-size: 16px;
                    color: #c7273a;
                    padding-top: 5px;
                }
            }
        }
    }
    .price-all-box {
        width: 100%;
        background: #fff;
        font-size: 14px;
        color: #313131;
        padding: 15px 15px;
        // margin-bottom: 10px;
        border-bottom: 2px solid #f0f0f0;
    }

    .submit-box {
        border-top: 1px solid #f0f0f0;
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 1000;
        padding: 12px 15px;
        width: 100%;
        background: #fff;
        display: flex;
        height: 62px;

        .right-box {
            margin-left: auto;
            display: flex;
            .like {
                width: 130px;
                margin-right: 10px;
                text-align: right;
                button {
                    height: 100%;
                    background: #353535;
                    border-color: #353535;
                    color: #fff;
                    font-size: 16px;
                    border-radius: 5px;
                    padding: 0 10px;
                }
            }
        }
        .how-much {
            padding-right: 10px;
            .all-price {
                font-size: 14px;
                color: #000;
                span {
                    font-size: 14px;
                    color: #c7273a;
                }
            }
            .prompt {
                text-align: right;
                color: #999;
                font-size: 14px;
            }
        }
        .submit {
            background: #c7273a;
            border-color: #c7273a;
            color: #fff;
            font-size: 16px;
            border-radius: 5px;
            padding: 0 20px;
        }
    }
}

.van-popup {
    .pop-ups {
        padding: 0 15px;

        .basic-info {
            position: relative;
            min-height: 120px;
            display: flex;
            border-bottom: 1px solid #f0f0f0;

            .thumb-img {
                border: 2px solid #fff;
                position: absolute;
                top: -20px;
                left: 0;
                width: 94px;
                height: 126px;
                img {
                    width: 100%;
                    height: 100%;
                }
            }
            .text-info {
                margin-left: 95px;
                padding: 5px 10px 0;
            }
            .price {
                .sale-price {
                    padding-right: 10px;
                    color: #c7273a;
                }
                .market-price {
                    color: #999;
                    text-decoration: line-through;
                }
            }
            .choosed-info {
                padding-top: 5px;
                color: #313131;
                font-size: 13px;
            }
        }

        .block-list {
            // height: auto;
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            // display: flex;
            // align-items: center;

            .name {
                font-size: 14px;
                color: #313131;
                padding-bottom: 10px;
            }

            ul {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            li {
                height: 28px;
                box-sizing: border-box;
                min-width: 50px;
                margin-right: 10px;
                border: 1px solid #313131;
                border-radius: 5px;
                padding: 5px 10px;
                text-align: center;
                font-size: 14px;
            }
            .active {
                background: #c7273a;
                border-color: #c7273a;
                color: #fff;
            }
        }

        .nums {
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            span {
                padding-right: 20px;
            }
        }
    }
    button {
        padding: 8px 0;
        width: 100%;
        text-align: center;
        background: #c7273a;
        font-size: 16px;
        color: #fff;
    }
}

:deep(.van-popup) {
    overflow-y: initial;
}
:deep(.van-nav-bar) {
    background: #373737;
}
:deep(.van-nav-bar .van-icon) {
    color: #fff;
}
:deep(.van-nav-bar__title) {
    font-weight: normal;
    color: #fff;
}
:deep(.van-nav-bar__text) {
    color: #fff;
}
:deep(.van-stepper__input) {
    width: 50%;
}
</style>
